package cn.luo.dao;

import cn.luo.entity.Blogs;
import cn.luo.entity.Content;
import cn.luo.entity.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class TagDao {
    private Logger logger = LoggerFactory.getLogger(TagDao.class);

    @Autowired
    MongoTemplate mongoTemplate;

    //查询全部类型
    public List<Tag> findAll(){
        Query query = new Query();
        List<Tag> lst = mongoTemplate.find(query,Tag.class);
        logger.info("lst==" + lst);
        return lst;
    }

    //添加类型
    public void addType(Tag tag){
        mongoTemplate.save(tag);
    }

    //修改数量
    public void update(String id){
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update();
        mongoTemplate.updateFirst(query,update.inc("num",1),Tag.class);
    }

    //根据条件查询
    public Tag findByName(String name){
        Query query = new Query(Criteria.where("name").is(name));
        Tag tag = mongoTemplate.findOne(query, Tag.class);
        return tag;
    }
}
